﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Experimental5 : BaseImageFilter
    {
        protected Dictionary<EXPERIMENTAL1, float> m_aMaxValue = new Dictionary<EXPERIMENTAL1, float>();
        protected EXPERIMENTAL1 M_MODE = EXPERIMENTAL1.BRIGHTNESS;
        protected float m_fThreshold = 1.0f;
        protected float m_fFactor = 1.0f;

        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental5"/> class.
        /// </summary>
        public Experimental5()
            : this(EXPERIMENTAL1.GRAYVALUE, 10, 1.0f, 1.0f)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental5"/> class.
        /// </summary>
        /// <param name="_experimental5">The _experimental5.</param>
        internal Experimental5(Experimental5 _experimental5)
        {
            this.M_MODE = _experimental5.M_MODE;
            this.m_fThreshold = _experimental5.m_fThreshold;
            this.m_fFactor = _experimental5.m_fFactor;
            this.m_fOffset = _experimental5.m_fOffset;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental5"/> class.
        /// </summary>
        /// <param name="_MODE">The _ MODE.</param>
        /// <param name="_fThreshold">The _f threshold.</param>
        /// <param name="_fFactor">The _f factor.</param>
        /// <param name="_fOffset">The _f offset.</param>
        public Experimental5(EXPERIMENTAL1 _MODE, float _fThreshold, float _fFactor, float _fOffset)
        {
            M_MODE = _MODE;
            m_fThreshold = _fThreshold;
            m_fFactor = _fFactor;
            m_fOffset = _fOffset;

            m_aMaxValue.Add(EXPERIMENTAL1.A, 255);
            m_aMaxValue.Add(EXPERIMENTAL1.B, 255);
            m_aMaxValue.Add(EXPERIMENTAL1.G, 255);
            m_aMaxValue.Add(EXPERIMENTAL1.R, 255);
            m_aMaxValue.Add(EXPERIMENTAL1.BRIGHTNESS, 1.0f);
            m_aMaxValue.Add(EXPERIMENTAL1.CHANNELSUM, 255 + 255 + 255);
            m_aMaxValue.Add(EXPERIMENTAL1.DEFAULT, 255);
            m_aMaxValue.Add(EXPERIMENTAL1.GRAYVALUE, 255);
            m_aMaxValue.Add(EXPERIMENTAL1.HUE, 360);
            m_aMaxValue.Add(EXPERIMENTAL1.LUMINANCE, 255);
            m_aMaxValue.Add(EXPERIMENTAL1.MUL, 255 * 255 * 255);
            m_aMaxValue.Add(EXPERIMENTAL1.SATURATION, 1.0f);
            m_aMaxValue.Add(EXPERIMENTAL1.SUM, 255 + 255);
            m_aMaxValue.Add(EXPERIMENTAL1.TOARGB, 0xFFFFFF);
        }

        /// <summary>
        /// Gets or sets threshold
        /// </summary>
        public float Threshold
        {
            get { return m_fThreshold; }
            set { m_fThreshold = value; }
        }

        /// <summary>
        /// Gets or sets factor
        /// </summary>
        public float Factor
        {
            get { return m_fFactor; }
            set { m_fFactor = value; }
        }

        /// <summary>
        /// Gets or sets the MODE.
        /// </summary>
        /// <value>The MODE.</value>
        public EXPERIMENTAL1 MODE
        {
            get { return M_MODE; }
            set { M_MODE = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            float fFactor = 1.0f;
            float fOffset = 0.0f;
            UnsafeBitmap bitmapCloned = new UnsafeBitmap((UnsafeBitmap)_bitmap.Clone());
            UnsafeBitmap bitmap = _bitmap;

            Color color = Color.Transparent;
            Color colorEnvironment = Color.Transparent;
            bitmap.Clear(Definitions.Black);

            float fDivisor = m_aMaxValue[M_MODE];

            ColorCalculation calculation = Definitions.ColorCalculationDictionary[M_MODE];

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    color = bitmapCloned.GetPixel(x, y);

                    int nX = 0;
                    int nY = 0;
                    foreach (Point ptPoint in Definitions.EnvironmentPointListClockwise)
                    {
                        nX = x + ptPoint.X;
                        nY = y + ptPoint.Y;

                        float fSum = 0;
                        if (nX < nWidth - 1 && nY < nHeight - 1 && nX > -1 && nY > -1)
                        {
                            colorEnvironment = bitmapCloned.GetPixel(nX, nY);
                            fSum += calculation(colorEnvironment);
                        }

                        if (fSum <= 0.0f) fSum = 1.0f;
                        float fGray = fDivisor / fSum * fFactor + fOffset;
                        bitmap.SetPixel(x, y, Color.FromArgb((int)fGray, (int)fGray, (int)fGray));
                    }
                }
            }
            bitmapCloned.Dispose();
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Experimental5(this);
        }
    }
}
